<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<p>
<?php 
//
include_once("..\\common.php");
$admin = false; 
CheckSalerLogon();
?>
      
  <?php
$clientid = $_GET["clientid"];
$salerid = $_SESSION["salerid"];

$posts = $_POST; 

foreach ($posts as $key => $value) {
	$posts[$key] = trim($value); 
} 
$DB = DBConnect();

$clienttype = $posts["clienttype"];
$clientname = $posts["clientname"];
$contact = $posts["contact"];
$next_devtime = $posts["next_devtime"];
$fundmoney = $posts["fundmoney"];
$demand = $posts["selectneed"];
$charact = $posts["selectcharacter"];
$devspeed = $posts["selectspeed"];
$othersalers = $posts["othersalers"];

$deal_time = $posts["dealtime"];
$deal_money = $posts["dealmoney"];
$addict_dev = isset($posts["addicdev"])?$posts["addicdev"]:0;
$suppose_saler = $posts["supposesalers"];
$product = $posts["product"];
$remark = $posts["remark"];

$submit_time = date("Y-m-d H:i:s");


if($clientname==""){ echo "客户名字不能为空!";  echo "<a href=\"clientform1.php?clientid="; echo $clientid; echo "\">后退</a>"; die(""); }
if($contact==""){ echo "客户联系方式不能为空！"; echo "<a href=\"clientform1.php?clientid="; echo $clientid; echo "\">后退</a>";  die(""); }
//add client
if($clientid < 0 )
{	
	//echo $clientname;
	$query = "INSERT INTO client SET type='$clienttype',name='$clientname',contact='$contact',
	next_devtime='$next_devtime',fund='$fundmoney',demand='$demand',
	charact='$charact',devspeed='$devspeed',othersalers='$othersalers',
	remark = '$remark',submit_time='$submit_time', createid=$salerid, deal_time='$deal_time',deal_money=$deal_money,addict_dev=$addict_dev,suppose_saler='$suppose_saler',product = '$product' ";
	//echo $query;
	
	mysql_query("START TRANSACTION",$DB);
	
	$result1 = mysql_query($query,$DB);
	$clientid=mysql_insert_id();
	//echo $clientid;
	
	$query ="INSERT INTO client_saler_rel SET salerid = '$salerid', clientid ='$clientid', starttime='$submit_time',  state='active' ";
	//echo $query;
	$result2 = mysql_query($query,$DB);	
	
	if ( $result1 && $result2 )
	{
		echo "添加成功!";	
		mysql_query("COMMIT",$DB);
		header("Location:index.php");
	}
	else 
	{
		mysql_query("ROLLBACK",$DB);
		die("添加失败!");	
	}
	
}
else //update client
{
	$query = "UPDATE client SET type='$clienttype',name='$clientname',contact='$contact',next_devtime='$next_devtime',fund='$fundmoney',demand='$demand',charact='$charact',devspeed='$devspeed',othersalers='$othersalers',remark = '$remark',deal_time='$deal_time',deal_money=$deal_money,addict_dev=$addict_dev,suppose_saler='$suppose_saler',product = '$product' WHERE id=$clientid ";
	echo $query;
	
	$result = mysql_query($query,$DB);
	
	
	if ($result)
	{
		echo "更新成功!";	
		header("Location:index.php");
	}
	else 
	{
		die("更新失败!");	
	}

}

?>


</body>
</html>